package com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.TenantContext;
import com.tech.platform.cms.databases.mybatis.sqlParser.tenantParser.exception.NullTenantException;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;

import java.util.Collections;
import java.util.List;

/**
 * @author shengqiang.wu
 * @date 2019/12/24/024 10:19
 * @desc
 */
@Setter
@Accessors(chain = true)
public class TenantColumnHandler implements TenantHandler{

    private boolean useMultipleTenantId = false;

    private List<String> filterTables = Collections.emptyList();

    /**
     * 从ThreadLocal 获取用户租户信息,适合查询,添加,修改和删除是使用（不适合查询中tenant in (a,b,c)）
     * @return
     */
    @Override
    public Expression getTenantId() {
        String tenant = TenantContext.threadLocal.get();
        if(StrUtil.isNotEmpty(tenant)){
            //字符串或者数据类型可以用StringValue兼容,适合 = 条件
            //如果处理是in 条件的,用ValueListExpression
            return new StringValue(tenant);
        }
        throw ExceptionUtils.mpe("use tenant model,must set tenant value,now tenant is null");
    }

    /**
     * 从ThreadLocal 获取用户租户信息,适合适合查询中tenant in (a,b,c),不适用添加,修改和删除
     * @return
     */
    public Expression getTenantId(boolean where){
        String tenant = TenantContext.threadLocal.get();
        if(StrUtil.isNotEmpty(tenant)){
            if(!useMultipleTenantId){
                return new StringValue(tenant);
            }

            if(where && useMultipleTenantId){
                InExpression inExpression = new InExpression();
                inExpression.setLeftExpression(new Column(getTenantIdColumn()));
                List<Expression> inValues = Collections.emptyList();
                String[] tenants = StrUtil.split(tenant,",");
                for (String s : tenants){
                    inValues.add(new StringValue(s));
                }
                inExpression.setRightItemsList(new ExpressionList(inValues));
                return inExpression;
            }
        }

        throw ExceptionUtils.mpe("use tenant model,must set tenant value,now tenant is null");
    }

    @Override
    public String getTenantIdColumn() {
        return "tenant_id";
    }

    @Override
    public boolean doTableFilter(String s) {
        if(CollUtil.isEmpty(filterTables)){
            return false;
        }

        for (String table : filterTables){
            if(StrUtil.equals(table,s)){
                return true;
            }
        }
        return false;
    }
}
